<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Scores extends Base{
    // 积分签到
    public function scoreIn()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $isSign=Db::name('score_bill')->where('user_id',$uid)->where('type',1)->where('time','>',strtotime(date('Y-m-d',time())))->find();
        if ($isSign) {
            return json(['code' => 502, 'msg' => '今日已签到！']);
        }
        $data['score']=Db::name('system_set')->where('id',1)->value('sign_score');
        Db::startTrans();
        try {
            $data['p_id']=0;
            $data['user_id']=$uid;
            $data['type']=1;
            $data['state']=1;
            $data['time']=time();
            $scoreId=Db::name('score_bill')->insertGetId($data);
            $res=Db::name('users')->where('id', $uid)->setInc('score',$data['score']);
            // 提交事务
            Db::commit();
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '提交事物失败！']);
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
    }
    
    public function isScorein()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $isSign=Db::name('score_bill')->where('user_id',$uid)->where('type',1)->where('time','>',strtotime(date('Y-m-d',time())))->find();
        if ($isSign) {
            return json(['code' => 200, 'msg' => '今日已签到！','state'=>1]);
        }else{
                return json(['code' => 500, 'msg' => '今日未曾签到','state'=>0]);
        }
    }
    // 积分产品下单
    public function goodsOrderin()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢']);
        }
        if(!empty(input("param.goods_id"))){
            $data['goods_id']=input("param.goods_id");
        }else{
            return json(['code' => 502, 'msg' => '产品id']);
        }
        if(!empty(input("param.score"))){
            $data['score']=input("param.score");
        }else{
            return json(['code' => 503, 'msg' => '积分单价']);
        }
        if(!empty(input("param.buy_num"))){
            $data['buy_num']=input("param.buy_num");
        }else{
            return json(['code' => 504, 'msg' => '购买数量']);
        }
        if(!empty(input("param.scores"))){
            $data['scores']=input("param.scores");
        }else{
            return json(['code' => 505, 'msg' => '支付积分']);
        }
        if(!empty(input("param.address_id"))){
            $data['address_id']=input("param.address_id");
        }else{
            return json(['code' => 506, 'msg' => '地址id']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $score=Db::name('users')->where('id', $uid)->value('score');
        if ($score<$data['scores']) {
            return json(['code' => 510, 'msg' => '积分不足']);
        }
        $data['user_id']=$uid;
        $data['order_no']=date('YmdHis',time()).mt_rand(1111,9999);
        $data['state']=1;
        $data['time']=time();
        $data['outtime']=time();//发货时间
        $data['uptime']=time();
        Db::startTrans();
        try {
            $orderId=Db::name('goods_orders')->insertGetId($data);
            Db::name('users')->where('id', $uid)->setDec('score',$data['scores']);
            Db::name('goods')->where('id', $data['goods_id'])->setDec('num',$data['buy_num']);
            $datascore['user_id']=$uid;
            $datascore['p_id']=$orderId;
            $datascore['score']=$data['scores'];
            $datascore['type']=3;
            $datascore['state']=1;
            $datascore['time']=time();
            $scoreId=Db::name('score_bill')->insertGetId($datascore);
            // 提交事务
            Db::commit();
            if($scoreId){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '提交事物失败！']);
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
    }


    // 积分明细
    public function scoreBill()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $map = [
        ];
        if(input("param.in_out")==1){
            array_push($map,['type','in',[1,2]]);
        }elseif(input("param.in_out")==2){
            array_push($map,['type','=',3]);
        }else{
            array_push($map,['type','<',4]);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('score_bill')->where('user_id',$uid)->where($map)->order('id desc')->limit(10*($page-1),10)->select();
        if ($res) {
            foreach ($res as $k => $v) {
                $res[$k]['time']=date('Y-m-d H:i:s',$res[$k]['time']);
                if($res[$k]['type']==1){
                    // 签到加积分
                    $res[$k]['img']='';
                    $res[$k]['title']='';
                }elseif($res[$k]['type']==2){
                    // 购物加积分
                    $product[$k]=Db::name('product')->where('id',$res[$k]['p_id'])->find();
                    $res[$k]['title']=$product[$k]['title'];
                    $res[$k]['img']=$product[$k]['img'];
                }elseif($res[$k]['type']==3){
                    // 兑换商品减积分
                    $product[$k]=Db::name('goods')->alias('a')->join('goods_orders b', 'a.id = b.goods_id','LEFT')->field('a.title,a.img,b.id as goods_orders_id')->where('b.id',$res[$k]['p_id'])->find();
                    $res[$k]['title']=$product[$k]['title'];
                    $res[$k]['img']=$product[$k]['img'];
                    $res[$k]['goods_orders_id']=$product[$k]['goods_orders_id'];
                }
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    // 我的积分订单详情
    public function myScoreOrderDetail()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '详情id呢']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('score_bill')->alias('a')->join('goods_orders b', 'a.p_id = b.id','LEFT')->join('goods c', 'b.goods_id = c.id','LEFT')->join('address d', 'b.address_id = d.id','LEFT')->field('a.*,b.score,b.buy_num,b.scores,b.outtime,b.confirm_time,b.express_company,b.express_no,b.state,c.title,c.img,d.name,d.phone,d.address')->where('a.user_id',$uid)->where('a.id',$id)->where('a.type',3)->find();
        if ($res) {
                $res['time']=date('Y-m-d H:i:s',$res['time']);
                if($res['outtime']!=0){
                    $res['outtime']=date('Y-m-d H:i:s',$res['outtime']); 
                }else{
                    $res['outtime']='';
                }
                if($res['confirm_time']!=0){
                    $res['confirm_time']=date('Y-m-d H:i:s',$res['confirm_time']); 
                }else{
                    $res['confirm_time']='';
                }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '失败！']);
        }
    }
    //已到货
    public function goodsFinish()
    {
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.goods_orders_id"))){
            $goods_orders_id=input("param.goods_orders_id");
        }else{
            return json(['code' => 502, 'msg' => '礼品订单id呢']);//p_id
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('goods_orders')->where('id',$goods_orders_id)->update(['state'=>3,'uptime'=>time(),'confirm_time'=>time()]);
        if ($res) {
            return json(['code' => 200, 'msg' => '成功！']);
        }else{
            return json(['code' => 500, 'msg' => '失败！']);
        }
    }
}